﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Global;
using CE.User;
public partial class car_insurance_AgentLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        General.ClearAllSession(this.Page, "ALL");
        if (Session[General.UserId] != null)
        {
            General.ClearAllSession(this.Page, "LOGOUT");
        }

        string _redirectUrl = string.Empty;
        //Sakthi on 09 Dec 2011 -- Start
        if (Request.Params["uid"] != null && Request.Params["guid"] != null && Request.Params["carid"] != null
           && Request.Params["customerid"] != null && Request.Params["carresultid"] != null && Request.Params["providercompanyid"] != null)
        {
            try
            {
                string _agentEmail = Request.Params["uid"].ToString();
                string _AgentGUID = Request.Params["guid"].ToString();

                Int64 _customerId = Convert.ToInt64(Request.Params["customerid"].ToString());

                if (CheckUserLoginCached(_customerId.ToString()))
                {
                    Account _agentAccount = new Account();
                    Account _customerAccount = new Account();
                    AccountDB _myAccountDB = new AccountDB();

                    Profile _agentProfile = new Profile();
                    Profile _customerProfile = new Profile();
                    ProfileDB _myProfileDB = new ProfileDB();

                    Contact _customerContact = new Contact();
                    ContactDB _myContactDB = new ContactDB();

                    _customerAccount = _myAccountDB.GetAccount(_customerId);
                    _agentAccount = _myAccountDB.CheckEmail(_agentEmail, 200);

                    if (_agentAccount != null && _customerAccount != null)
                    {
                        if (_myAccountDB.GetGUID(_agentAccount.CEID).ToUpper() == _AgentGUID.ToUpper())
                        {
                            _customerProfile = _myProfileDB.GetProfile(_customerId);
                            _customerContact = _myContactDB.GetContact(_customerId);


                            Int64 _carID = Convert.ToInt64(Request.Params["carid"].ToString());
                            CE.CarInsurance.CarPolicy _carpolicy = new CE.CarInsurance.CarPolicy();
                            CE.CarInsurance.CarPolicyDB _carpolicydb = new CE.CarInsurance.CarPolicyDB();
                            _carpolicy = _carpolicydb.GetCarPolicy(_carID, _customerId);

                            if (_carpolicy != null)
                            {
                                Session[General.UserId] = _customerId;
                                Session[General.UserTypeId] = _customerAccount.UserTypeId.ToString();
                                Session[General.CarId] = _carID;
                                Session[General.CarPolicyId] = _carpolicy.CECarPolicyId.ToString();
                                Session[General.CarResultId] = Request.Params["carresultid"].ToString();
                                Session[General.ProviderCompanyId] = Request.Params["providercompanyid"].ToString();
                                Session[General.AgentId] = _agentAccount.CEID.ToString();

                                string _customerName = "";
                                string _customerMobileNo = "";

                                if (_customerProfile != null)
                                {
                                    _customerName = _customerProfile.SurName;
                                    if (_customerProfile.FirstName.Length > 0)
                                    {
                                        _customerName += " " + _customerProfile.FirstName;
                                    }
                                }

                                if (_customerContact != null)
                                {
                                    _customerMobileNo = _customerContact.Mobile;
                                }

                                Session[General.UserName] = _customerName;
                                Session[General.UserPhoneNo] = _customerMobileNo;
                                Session[General.UserEmail] = _customerAccount.Email.ToString();
                                Session[General.AgentBuyOnly] = "1";

                                if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 11)
                                {
                                    Response.Redirect("proposalforms/azcp_proposal_form.aspx");
                                }
                                else if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 12)
                                {
                                    Response.Redirect("proposalforms/msig_proposal_form.aspx");
                                }
                                else if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 16)
                                {
                                    Response.Redirect("proposalforms/axa_proposal_form.aspx");
                                }
                                else if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 29)
                                {
                                    Response.Redirect("proposalforms/lmg_proposal_form.aspx");
                                }
                                else if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 31)
                                {
                                    Response.Redirect("proposalforms/chartis_proposal_form.aspx");
                                }
                                else if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 32)
                                {
                                    Response.Redirect("proposalforms/bki_proposal_form.aspx");
                                }
                                else if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 33)
                                {
                                    Response.Redirect("proposalforms/mti_proposal_form.aspx");
                                }
                                else if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 41)
                                {
                                    Response.Redirect("proposalforms/kurnia_proposal_form.aspx");
                                }
                                else if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 46)
                                {
                                    Response.Redirect("proposalforms/tms_proposal_form.aspx");
                                }
                            }
                            else
                            {
                                lblMessage.Text = "Invalid Car Quote ID.";
                            }
                            //}
                            //else
                            //{
                            //    lblMessage.Text = "Customer is currently online. Please close this window and try again later.";
                            //}

                        }
                        else
                        {
                            lblMessage.Text = "Invalid Agent ID.";
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Invalid Customer ID or Agent ID.";
                    }
                }
                else
                {
                    _redirectUrl = CE.Global.General.GetUrl("~/user/usermessage.aspx", Page);
                    Session["UMMSG"] = "UM,1993";
                    Response.Redirect(_redirectUrl);
                }

            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
                lblMessage.Text = "<br/><br/><b>[Error Code:1001]Please close this page.</b>";
            }
        }
        //Sakthi on 09 Dec 2011 -- End
        else if (Request.Params["uid"] != null &&
            //Request.Params["uname"] != null && 
            Request.Params["cid"] != null &&
            //Request.Params["cemail"] != null &&
            //Request.Params["cphone"] != null &&
            // Request.Params["cname"] != null &&
            Request.Params["pcid"] != null)
        {
            try
            {
                Int64 _agentId = Convert.ToInt64(Request.Params["uid"].ToString());
                Int64 _customerId = Convert.ToInt64(Request.Params["cid"].ToString());
                Int64 _providerCompanyId = Convert.ToInt64(Request.Params["pcid"].ToString());
                if (CheckUserLoginCached(_customerId.ToString()))
                {
                    Account _agentAccount = new Account();
                    Account _customerAccount = new Account();
                    AccountDB _myAccountDB = new AccountDB();

                    Profile _agentProfile = new Profile();
                    Profile _customerProfile = new Profile();
                    ProfileDB _myProfileDB = new ProfileDB();

                    Contact _customerContact = new Contact();
                    ContactDB _myContactDB = new ContactDB();

                    _agentAccount = _myAccountDB.GetAccount(_agentId);
                    _customerAccount = _myAccountDB.GetAccount(_customerId);

                    if (_agentAccount != null && _customerAccount != null)
                    {
                        _agentProfile = _myProfileDB.GetProfile(_agentId);
                        _customerProfile = _myProfileDB.GetProfile(_customerId);

                        _customerContact = _myContactDB.GetContact(_customerId);
                    }

                    string _agentName = "";
                    string _customerName = "";
                    string _customerEmail = "";
                    string _customerMobileNo = "";

                    if (_agentProfile != null)
                    {
                        _agentName = _agentProfile.SurName;
                        if (_agentProfile.FirstName.Length > 0)
                        {
                            _agentName += " " + _agentProfile.FirstName;
                        }
                    }

                    if (_customerProfile != null)
                    {
                        _customerName = _customerProfile.SurName;
                        if (_customerProfile.FirstName.Length > 0)
                        {
                            _customerName += " " + _customerProfile.FirstName;
                        }
                    }

                    if (_customerContact != null)
                    {
                        _customerEmail = _customerContact.Email;
                        _customerMobileNo = _customerContact.Mobile;
                    }

                    Session[General.AgentId] = _agentId.ToString();
                    Session[General.AgentName] = _agentName;
                    Session[General.UserId] = _customerId.ToString();
                    Session[General.UserName] = _customerName;
                    Session[General.UserEmail] = _customerEmail;
                    Session[General.UserPhoneNo] = _customerMobileNo;
                    Session[General.UserTypeId] = "400";
                    if (_providerCompanyId > 0)
                    {
                        Session[General.ProviderCompanyId] = _providerCompanyId.ToString();
                    }
                    Session[General.UserProfileAndContactComplete] = "1";

                    string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsCXLive");
                    if (_strCXLive == "0")
                    {
                        Session[CE.Global.General.Guest] = "User";
                    }

                    if (Request.Params["carid"] != null &&
                    Request.Params["policyid"] != null)
                    {
                        Int64 _carId = 0;
                        Int64 _policyId = 0;
                        if (Int64.TryParse(Request.Params["carid"].ToString(), out _carId) && Int64.TryParse(Request.Params["policyid"].ToString(), out _policyId))
                        {
                            CE.CarInsurance.CarPolicy _carpolicy = new CE.CarInsurance.CarPolicy();
                            CE.CarInsurance.CarPolicyDB _carpolicydb = new CE.CarInsurance.CarPolicyDB();
                            _carpolicy = _carpolicydb.GetCarPolicy(_carId, _policyId, _customerId);
                            if (_carpolicy != null)
                            {
                                Session[General.CarId] = Request.Params["carid"].ToString();
                                Session[General.CarPolicyId] = Request.Params["policyid"].ToString();

                                if (_carpolicy.InsuranceType == 10)
                                {
                                    Response.Redirect("class-1.aspx");
                                }
                                else if (_carpolicy.InsuranceType == 11)
                                {
                                    Response.Redirect("class-1single.aspx");
                                }
                                else if (_carpolicy.InsuranceType == 21)
                                {
                                    Response.Redirect("class-2plus.aspx");
                                }
                                else if (_carpolicy.InsuranceType == 30)
                                {
                                    Response.Redirect("class-3.aspx");
                                }
                                else if (_carpolicy.InsuranceType == 31)
                                {
                                    Response.Redirect("class-3plus.aspx");
                                }
                            }
                            else
                            {
                                Response.Redirect("agentLogin.aspx");
                            }
                        }
                        else
                        {
                            Response.Redirect("agentLogin.aspx");
                        }

                    }
                    else
                    {
                        Response.Redirect("class-1.aspx");
                    }
                }
                else
                {
                    _redirectUrl = CE.Global.General.GetUrl("~/user/usermessage.aspx", Page);
                    Session["UMMSG"] = "UM,1993";
                    Response.Redirect(_redirectUrl);
                }
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
                lblMessage.Text = "<br/><br/><b>[Error Code:2001]Please close this page.</b>";
            }
        }
        else if (Request.Params["uid"] != null && Request.Params["guid"] != null && Request.Params["pcid"] != null)
        {
            try
            {
                string _guid = Request.Params["guid"].ToString();
                Int64 _agentId = Convert.ToInt64(Request.Params["uid"].ToString());
                Int64 _providerCompanyId = Convert.ToInt64(Request.Params["pcid"].ToString());

                string _agentName = "";
                Account _account = new Account();
                AccountDB _accountdb = new AccountDB();
                _account = _accountdb.GetAccount(_agentId);
                if (_account != null)
                {
                    _agentId = _account.CEID;
                    Profile _agentProfile = new Profile();
                    ProfileDB _agentProfileDB = new ProfileDB();
                    _agentProfile = _agentProfileDB.GetProfile(_agentId);
                    if (_agentProfile != null)
                    {
                        _agentName = _agentProfile.SurName;
                        if (_agentProfile.FirstName.Length > 0)
                        {
                            _agentName += " " + _agentProfile.FirstName;
                        }
                    }
                    string _agentGuid = _accountdb.GetGUID(_account.CEID);
                    if (_guid.ToUpper() == _agentGuid.ToUpper())
                    {
                        _account = null;
                        _accountdb = null;
                        _agentProfile = null;
                        _agentProfileDB = null;
                        string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsCXLive");
                        if (_strCXLive == "0")
                        {
                            Session[CE.Global.General.Guest] = "User";
                        }

                        Session[General.AgentId] = _agentId.ToString();
                        Session[General.AgentName] = _agentName;
                        //Session[General.ProviderCompanyId] = Request.Params["pcid"].ToString();
                        if (_providerCompanyId > 0)
                        {
                            Session[General.ProviderCompanyId] = _providerCompanyId.ToString();
                        }

                        Response.Redirect("class-1.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/");
                }
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
                lblMessage.Text = "<br/><br/><b>[Error Code:3001]Please close this page.</b>";
            }
        }
        else
        {
            lblMessage.Text = "Please close this page.";
        }

    }

    private bool CheckUserLoginCached(string UserId)
    {
        bool _retValue = false;
        string sKey = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.UserCacheName");
        sKey += UserId;
        if (HttpContext.Current.Cache[sKey] == null)
        {
            _retValue = true;
        }
        else
        {
            _retValue = false;
        }
        return _retValue;
    }
}
